<!DOCTYPE html> 
<html>
	<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1"> 
	<title>OWL FrontEnd - search</title> 
	<link rel="stylesheet"  href="./jquery.mobile-1.1.0/jquery.mobile-1.1.0.css" />  
	<link rel="stylesheet"  href="./jquery.mobile-1.1.0/jquery.mobile.theme-1.1.0.min.css"></script>
	<!--link rel="stylesheet" href="./codemirror/lib/codemirror.css"/-->
	<link rel="stylesheet" href="./css/owl.css"/>

	<script src="./js/jquery-1.7.2.min.js"></script>
	<!--script src="./../docs/_assets/js/jqm-docs.js"></script-->
	<script src="./jquery.mobile-1.1.0/jquery.mobile-1.1.0.min.js"></script>
	<!--script src="./codemirror/lib/codemirror.js"></script>
	<script src="./codemirror/mode/clike/clike.js"></script-->

	<script >
	/*global $:false, jQuery:false, console:false */
	$(function () {
		"use strict";
		// Stuff to do as soon as the DOM is ready. Use $() w/o colliding with other libs;
		$("#search-basic").keyup(function (e) {
			var input = $('#search-basic').val();
			if (input.length >= 3) {
				ajaxCall(input, 0);
			}
			//$("#output").html(input);
		});

		function updateSlider(val, total) {
			// rebuild the while thing as jquery mobile is not very 'dynamic'
			var $max=-1, $val=-1;
			var $slider = $('#slider-fill');
			if ($slider.length>0)
			{
				$max = $slider.attr('max');
				$val = $slider.val();
			}
			if (($max==-1)||($max!=total))
			{
				$slider.unbind('change');

				var html = "";

				html += "<label for=\"slider-fill\">Input slider:</label>";
				html += "<input type=\"range\" name=\"slider\" id=\"slider-fill\" ";
				html += "value=\""+val+"\" min=\"0\" max=\""+total+"\" step=\"10\" data-highlight=\"true\" />";
				html += "<span class=\"ui-li-count\">"+total+"</span>";
				$("#slider-container").html(html);
				$slider = $("#slider-fill").slider();

				$slider.bind('change', function(e) {
					var start = $('#slider-fill').val();
					var input = $('#search-basic').val();
					console.log('slider-change : '+start);
					if (input.length>=3)
						ajaxCall(input, start);
				});

				console.log($slider.data("events"));
			}
			else if ($val!=val)
			{
				//$('#slider-fill').val(val).slider('refresh');
			}
		}
		var $xhr = null;
		function ajaxCall(input,start) {
			console.log('search '+input+' from '+ start);
			var $sig = parseInt($('#results').attr('sig'),10);
			if (isNaN($sig))
				$sig = 0;
			var d = new Date();
			var t = d.getTime();
			if ($xhr !== null)
				$xhr.abort();
			$xhr = $.getJSON(
				//"/search.c?callback=?", //calback id need to be propagated return msg
				"/search.c",
				{
					sig: t,
					search: input,
					start: start,
					count: "10"
				},
			function(data) {
				$xhr = null;
				var $sig = parseInt($('#results').attr('sig'),10);
				if (isNaN($sig))
					$sig = 0;
				if (parseInt(data.sig,10) <= $sig)
					return;
				$('#results').attr('sig', data.sig);
				$('#results').children().remove('li');
				updateSlider(data.start, data.total);
				$.each(data.hits, function(i,item){
					$("<li></li>")
						.append("<a href=\"#\" onclick=\"jQuery.ajax('open.c/"+item.id+"'); return false;\">"+
						"<h3>"+item.index+" - "+item.name+"</h3></a>"+
						"<p style=\"font-family:monospace;padding-left:24px\">"+
						(item.extracts.length>0?item.extracts[0].line+': '+item.extracts[0].text+"</br>":"")+
						(item.extracts.length>1?item.extracts[1].line+': '+item.extracts[1].text+"</br>":"")+
						(item.extracts.length>2?item.extracts[2].line+': '+item.extracts[2].text+"</br>":"")+
						(item.extracts.length>3?item.extracts[3].line+': '+item.extracts[3].text:"")+
						"</p>"+
						"<p><a href=\"#\" onclick=\"jQuery.ajax('explore.c/"+item.id+"'); return false;\">"+item.path+"</a></p>"+
						"<p class=\"ui-li-aside\"><strong>"+item.size+"</strong></p>"+
						"</li>"
						).appendTo("#results");

				});
				$("#results").listview('refresh');
				/*
				$("textArea").each(function (i, el) {
					var editor = CodeMirror.fromTextArea(el, {
	        			lineNumbers: false,
	        			indentUnit: 1,
	        			readOnly: true,
	        			mode: "text/x-csrc"
	      			});
	      			editor.indentLine(0, false);
	      			editor.indentLine(1, false);
	      			editor.indentLine(2, false);
	      			editor.indentLine(3, false);
	      			editor.refresh();
	      		});*/ // each
			});
		} //ajaxCall
	});
	</script>

</head> 
<body> 

	<div data-role="page" class="type-interior">

		<div data-role="header" data-theme="f">
		<h1>OWL Code Search</h1>
			<!--a href="../../" data-icon="home" data-iconpos="notext" data-direction="reverse">Home</a>
			<a href="./nav.html" data-icon="search" data-iconpos="notext" data-rel="dialog" data-transition="fade">Search</a-->
		</div><!-- /header -->
		
	<div data-role="content">
		<div class="content-primary">
			<div>
				<label for="search-basic">Search Input:</label>
				<input type="search" name="search" id="search-basic" value="" />	
<div id="slider-container">
	<label for="slider-fill">Input slider:</label>
	<input type="range" name="slider" id="slider-fill" value="60" min="0" max="100" data-highlight="true" />
</div>
				<p/>			
			</div>
		<ul data-role="listview" data-theme="d" data-divider-theme="d" id="results">
			<li data-role="list-divider">About <span class="ui-li-count">2</span>
			</li>
			<li><a href="about.html?">				
					<h3>Welcome to owl</h3>
					<p><strong>loren ipsum</strong></p>
					<p>welcome you !</p>
					<p class="ui-li-aside"><strong>:-)</strong></p>				
			</a></li>
		</ul>
		</div><!--/content-primary -->		

		<div class="content-secondary">
			<div data-role="collapsible" data-collapsed="true" data-theme="b" data-content-theme="d">
					<!--h3>More in this section</h3>
					<ul data-role="listview" data-theme="c" data-dividertheme="d">
						<li data-role="list-divider">List views</li>
						<li><a href="docs-lists.html">List basics &amp; API</a></li>
						<li><a href="lists-ul.html">Basic linked list</a></li>
						<li><a href="lists-nested.html">Nested list</a></li>
						<li><a href="lists-ol.html">Numbered list</a></li>
					</ul-->
					<img src='./images/owl.png'/>
			</div>
		</div>		

	</div><!-- /content -->

	<div data-role="footer" class="footer-docs" data-theme="c">
			<p>&copy; owl</p>
	</div>

	</div><!-- /page -->

	</body>
	</html>
